import { defineStore } from 'pinia';
import { goodsList } from '@/api/goods/goods';
import { exhibitionData, teamContentInfo } from '@/api/templates/templates';
import { investigationList } from '@/api/investigation/investigation';
import { commentList } from '@/api/comment/comment';
import { companysData, modulesData } from '@/api/templates/templates';
import { copyrightInfo } from '@/api/templates/templates';
import { settings } from '@/api/project/project';
import { moduleShowpieces } from '@/api/exhibit/exhibit';
import { exhibitShowpiece } from '@/api/templates/templates';
import { store } from '@/store';
import { treeList } from '@/api/user/user';
import { contentData } from '@/api/banner/banner';
import { templateIsFollow } from '@/api/project/project';

interface CulturalType {
  value?: string;
  pageNo: number;
  pageSize: number;
}
interface ExhibitionType {
  value?: string;
  typeId: number;
  ageId: number;
  levelId: number;
  ModuleId: number;
  CompanyId: number;
  pageNo: number;
  pageSize: number;
}
interface ResearchType {
  value?: string;
  pageNo: number;
  pageSize: number;
  typeId?: number;
}
interface MessageListType {
  CommentStatus: number;
  showpieceId: number; //展品主键
  projectId: number; //项目主键
  value?: string; //关键字
  pageNo: number;
  pageSize: number;
  isSelf: number;
}
interface ExhibitionDetailsListType {
  pageSize: number;
  pageNo: number;
  id: number;
  value?: string;
}
export const commonDataStore = defineStore('commonData', {
  state: () => ({
    isStart: false, //是否关注
    CopyrightDescription: '', //版权声明
    CulturalTotal: 0, //文创总数
    CulturalList: [], //文创商品列表
    ExhibitionTotal: 0, //展品总数
    ExhibitionList: [], //展品列表
    ResearchTotal: 0, //学术研究总数
    ResearchList: [], //学术研究列表
    MessageTotal: 0, //评论总数
    MessageList: [], //评论列表
    levelList: [], //级别列表
    moduleList: [], //单元列表
    companyList: [], //单元列表
    ageList: [], //年代列表
    typeList: [], //类型列表
    copyrightInfo: '', //版权声明信息
    ExhibitionDetailsList: [], //展品详情相关展品列表
    ExhibitionDetails: {}, //展品详情信息
    teamInfo: {
      text: '',
      value: '',
    }, //策展团队信息
    menuList: [
      {
        config: false,
        type: 3,
      },
      {
        config: false,
        type: 4,
      },
      {
        config: false,
        type: 5,
      },
      {
        config: false,
        type: 2,
      },
    ], //菜单控件状态 type2:留言板  type3:学术研究  type4:文创商品  type5: 版权声明 config为true时为开启状态
  }),
  getters: {},
  actions: {
    // isStart
    async setisStart() {
      templateIsFollow(localStorage.getItem('projectId')).then((res) => {
        if (res.statusCode == 200) {
          this.isStart = res.data;
        }
      });
    },
    startCurrent(e: boolean) {
      this.isStart = e;
    },
    async GetCopyrightInfo() {
      let { data }: any = await contentData(6); //类型

      this.copyrightInfo = data.data; //获取
    },
    async getCultural(parmas: CulturalType) {
      //获取文创相关列表
      let res: any = await goodsList(localStorage.getItem('projectId'), parmas); //类型
      this.CulturalTotal = res.data.total; //获取文创总数
      this.CulturalList = res.data.data; //获取文创列表
    },
    async getExhibition(parmas: ExhibitionType) {
      //获取展品相关列表
      let res: any = await exhibitionData(localStorage.getItem('projectId'), parmas); //类型
      this.ExhibitionTotal = res.data.total; //获取展品总数
      this.ExhibitionList = res.data.data; //获取展品列表
    },
    async getResearch(parmas: ResearchType) {
      //获取学术研究相关
      let res: any = await investigationList(localStorage.getItem('projectId'), parmas); //类型
      this.ResearchTotal = res.data.total; //获取展品总数
      this.ResearchList = res.data.data; //获取展品列表
      this.ResearchList.forEach((element) => {
        element.createDate = element.createDate.substring(0, 10);
      });
    },
    async getTeamContentDescription() {
      //获取策展团队声明
      let { data } = await teamContentInfo(localStorage.getItem('projectId'));
      if (data) {
        if (data.value) {
          data.text = data.text + `<p>特别鸣谢单位：${data.value}</p>`;
        }
        this.teamInfo = data;
      }
    },
    async getMessageList(parmas: MessageListType) {
      //获取评论相关
      let { data } = await commentList(parmas);
      this.MessageTotal = data.total; //获取展品总数
      this.MessageList = data.data; //获取展品列表
    },
    async getMinxinList() {
      let res = await treeList(371994471206981); //级别
      let res2 = await treeList(371999507447877); //类型
      let res3 = await treeList(372051821408325); //年代

      this.levelList = res.data; //获取年代数组
      this.ageList = res3.data; //获取年代数组
      this.typeList = res2.data; //类型数组
    },
    async getMinxinList1() {
      let { data } = await companysData(localStorage.getItem('projectId')); //单位列表
      let DataList = await modulesData(localStorage.getItem('projectId')); //单元列表

      this.moduleList = DataList.data; //单元数组
      this.companyList = data; //单位数组
    },
    async getMenuList() {
      //获取菜单列表
      try {
        let res = await settings(localStorage.getItem('projectId'), 3);
        this.menuList = res.data; //获取菜单列表
        console.log('ssssssssssssssss', this.menuList);
      } catch (err) {
        // alert(err);
      }
    },
    async getCopyrightDescription() {
      //获取版权声明
      let { data } = await copyrightInfo(localStorage.getItem('projectId'));
      if (data) {
        this.CopyrightDescription = data;
      }
    },
    async getExhibitionDetails(moduleId: number, showpieceid: number) {
      //moduleId单元id
      //展品id
      let { data } = await exhibitShowpiece(moduleId, showpieceid);

      this.ExhibitionDetails = data;
      this.ExhibitionDetails.images.unshift({
        data: this.ExhibitionDetails.coverUrl,
        type: -1,
      });
    },
    async getExhibitionDetailsList(parmas: ExhibitionDetailsListType) {
      //关联展品列表
      moduleShowpieces(localStorage.getItem('projectId'), parmas).then((res) => {
        this.ExhibitionDetailsList = res.data.data;
      });
    },
    resetting() {
      //重置所有参数
      this.CulturalTotal = 0;
      this.CulturalList = [];
      this.ExhibitionTotal = 0;
      this.ExhibitionList = [];
      this.ResearchTotal = 0;
      this.ResearchList = [];
      this.MessageTotal = 0;
      this.MessageList = [];
      this.levelList = [];
      this.moduleList = [];
      this.companyList = 0;
      this.ageList = [];
      this.typeList = [];
    },
  },
  persist: true,
});
export function getCommonDataStore() {
  return commonDataStore(store);
}
